﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace WpfApp2
{
    public class RequiredRule : ValidationRule
    {
        private int minmunLength = -1;
        private int maxmunLength = -1;
        private string errorMessage;
        public int MinmunLength
        {
            get { return minmunLength; }
            set { minmunLength = value; }
        }
        public int MaxmunLength
        {
            get { return maxmunLength; }
            set { maxmunLength = value; }
        }
        public string ErrorMessage
        {
            get { return errorMessage; }
            set { errorMessage = value; }
        }
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            ValidationResult result = new ValidationResult(true, null);
            if (value == null)
                result = new ValidationResult(false, "不能为空值！");
            if (string.IsNullOrEmpty(value.ToString()))
                result = new ValidationResult(false, "不能为空字符串");
            string inputstring = (value ?? string.Empty).ToString();
            if (inputstring.Length < this.minmunLength || (this.maxmunLength > 0 && inputstring.Length > this.maxmunLength))
                result = new ValidationResult(false, this.errorMessage);
            return result;
        }
    }
}
